﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 线性表链表
{
    internal class HeadLinkedList
    {
        public Node Head { get; set; }
        public void Init()
        {
            Head  = new Node();
            Node node1 = new Node { Value = 1 };
            Node node2 = new Node { Value = 2 };
            Node node3 = new Node { Value = 3 };
            Node node4 = new Node { Value = 4 };
            Node node5 = new Node { Value = 5 };
            Head.Next = node1;
            node1.Next = node2;
            node2.Next = node3;
            node3.Next = node4;
            node4.Next = node5;
        }

        public void Insert(int key,Node node)
        {
            if(key < 1)
            {
                Console.WriteLine("索引不合法");
                return;
            }

            Node temp = Head;
            int index = 0;
            while(temp.Next != null && index<key-1)
            {
                index++;
                temp = temp.Next;
                
            }
            node.Next = temp.Next; 
            temp.Next = node;
        }

        public void Print()
        {
            Node node = Head.Next;
            while (node != null)
            {
                Console.Write (node.Value +"\t");
                node = node.Next;
            }
            Console.WriteLine();
        }
    }
}
